home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / lib / Exception.h < prev    next >
C/C++ Source or Header  |  1990-05-19  |  3KB  |  139 lines

  1. #ifndef    EXCEPTION_H
  2. #define    EXCEPTION_H
  3.  
  4. /*$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/Exception.h,v 3.0 90/05/20 00:19:31 kgorlen Rel $*/
  5.  
  6. /* Exception.h -- declarations for NIHCL exception handling
  7.  
  8.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  9.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  10.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  11.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  12.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  13.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  14.  
  15. Author:
  16.     K. E. Gorlen
  17.     Computer Systems Laboratory, DCRT
  18.     National Institutes of Health
  19.     Bethesda, MD 20892
  20.  
  21. $Log:    Exception.h,v $
  22.  * Revision 3.0  90/05/20  00:19:31  kgorlen
  23.  * Release for 1st edition.
  24.  * 
  25. */
  26.  
  27. #include "Object.h"
  28. #include <setjmp.h>
  29.  
  30. class ExceptionAction : public NIHCL {
  31. public:            // type definitions
  32.     enum exceptionActionTy { ABORT, RAISE };
  33. private:
  34.     unsigned error_code;
  35.     exceptionActionTy old_action;
  36. public:
  37.     ExceptionAction(unsigned error);
  38.     virtual ~ExceptionAction();
  39. };
  40.  
  41. class RaiseException : public ExceptionAction {
  42. public:
  43.     RaiseException(unsigned error);
  44. };
  45.  
  46. class AbortException : public ExceptionAction {
  47. public:
  48.     AbortException(unsigned error);
  49. };
  50.  
  51. #ifndef NESTED_TYPES
  52. typedef void (*exceptionTrapTy)(unsigned&, int&, ...);
  53. #endif
  54.  
  55. class ExceptionTrap : public NIHCL {
  56. public:            // type definitions
  57. #ifdef NESTED_TYPES
  58.     typedef void (*exceptionTrapTy)(unsigned&, int&, ...);
  59. #endif
  60. private:
  61.     exceptionTrapTy old_trap;
  62. public:
  63.     ExceptionTrap(exceptionTrapTy xtrap =NULL);
  64.     ~ExceptionTrap();
  65. };
  66.  
  67. class Process;
  68. class Catch;
  69.  
  70. class ExceptionEnv : public NIHCL {
  71. private:        // static member variables
  72.     static ExceptionEnv* stackTop;
  73.     static ExceptionEnv* lastResort;
  74.     friend Catch;
  75.     friend void RAISE(int exception);
  76.     friend Process;
  77. private:
  78.     ExceptionEnv* prev;
  79.     int exceptionCode;
  80.     jmp_buf env;
  81. public:
  82.     ExceptionEnv() {    // MUST be inline
  83.         prev = stackTop;
  84.         stackTop = this;
  85.         exceptionCode = setjmp(env);
  86.     }
  87.     ~ExceptionEnv() { if (stackTop == this) pop(); }
  88.     int code() const { return exceptionCode; }
  89.     void pop()    { stackTop = prev; }
  90.     void raise(int exception);
  91. };
  92.  
  93. #define    EXCEPTION_CODE    exception_environment.code()
  94.  
  95. #define BEGINX { \
  96.     ExceptionEnv exception_environment; \
  97.     if (EXCEPTION_CODE == 0) { \
  98.         
  99. // Statements in the scope of this exception handler block go here.
  100.  
  101. #define EXCEPTION \
  102.     } \
  103.     else switch(EXCEPTION_CODE) { \
  104.  
  105. /*
  106. Exception handlers go here; the syntax is that of a switch statement
  107. body.  The exception code that caused this EXCEPTION block to be entered
  108. may be accessed via the macro EXCEPTION_CODE.  The statement
  109. "default:RAISE(EXCEPTION_CODE);" will propagate the current exception up
  110. to the next exception handler block if the exception is not handled by
  111. this block; otherwise, execution continues with the first statement
  112. after this exception block.
  113. */
  114.  
  115. #define ENDX \
  116.     }; \
  117. } \
  118.  
  119. inline void RAISE(int exception)
  120. {
  121.     ExceptionEnv::stackTop->raise(exception);
  122. }
  123.  
  124. class Catch : public NIHCL {
  125.     static Catch* stackTop;    // top of catch frame stack
  126.     friend Process;
  127. private:
  128.     Catch*    next;
  129.     Catch*    prev;
  130.     Object*    obj;
  131.     friend ExceptionEnv;
  132. public:
  133.     Catch(Object*);
  134.     Catch();        // for construction of catch_stack_top only!
  135.     ~Catch();
  136. };
  137.  
  138. #endif
  139.